package com.hengda.museumonline.ui;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioGroup;

import com.amap.api.maps.TextureMapView;
import com.hengda.museumonline.R;
import com.hengda.museumonline.adapter.ContentAdapter;
import com.hengda.museumonline.base.ArchPager;
import com.hengda.museumonline.base.EncPager;
import com.hengda.museumonline.base.InfoPager;
import com.hengda.museumonline.base.MuseumPager;
import com.hengda.museumonline.base.PcrPager;
import com.hengda.museumonline.rxbus.RxBus;
import com.hengda.museumonline.view.NoScrollViewPager;

import java.util.ArrayList;

import rx.Observable;
import rx.functions.Action1;

public class MainActivity extends BaseActivity {

    private NoScrollViewPager vpcontent;
    private android.widget.RadioGroup rggroup;
    private ArrayList<View> mPagers;
    private Observable observable;
    private TextureMapView textureMapView;
    private MuseumPager museumPager;
    private ArchPager archPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.rggroup = (RadioGroup) findViewById(R.id.rg_group);
        this.vpcontent = (NoScrollViewPager) findViewById(R.id.vp_content);
        textureMapView = new TextureMapView(this);
        textureMapView.onCreate(savedInstanceState);
        mPagers = new ArrayList<>();
        InfoPager infoPager = new InfoPager(MainActivity.this);
        archPager = new ArchPager(MainActivity.this, savedInstanceState);
        museumPager = new MuseumPager(MainActivity.this, savedInstanceState);
        PcrPager pcrPager = new PcrPager(MainActivity.this);
        EncPager encPager = new EncPager(MainActivity.this);

        // 添加五个标签页
        mPagers.add(infoPager.view);
        mPagers.add(archPager.view);
        mPagers.add(museumPager.view);
        mPagers.add(pcrPager.view);
        mPagers.add(encPager.view);
        vpcontent.setAdapter(new ContentAdapter(mPagers));
        // 底栏标签切换监听
        rggroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_info:
                        vpcontent.setCurrentItem(0, false);
                        break;
                    case R.id.rb_arch:
                        vpcontent.setCurrentItem(1, false);
                        break;
                    case R.id.rb_museum:
                        vpcontent.setCurrentItem(2, false);
                        break;
                    case R.id.rb_pcr:
                        vpcontent.setCurrentItem(3, false);
                        break;
                    case R.id.rb_enc:
                        vpcontent.setCurrentItem(4, false);
                        break;
                    default:
                        break;
                }
            }
        });
        vpcontent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                museumPager.listUtil.dismissPop();
                museumPager.ivMap.setImageResource(R.drawable.arch_list);
                museumPager.temp = true;
                if (archPager.mPopWindow != null && archPager.mPopWindow.isShowing()) {
                    archPager.mPopWindow.dismiss();
                    archPager.ivMap.setImageResource(R.drawable.arch_list);
                    archPager.temp = true;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        observable = RxBus.get().register("MainActivity", null);
        observable.subscribe(new Action1() {
            @Override
            public void call(Object o) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        textureMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        textureMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        textureMapView.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister("MainActivity", observable);
        textureMapView.onDestroy();
    }
}
